/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} targetSum
 * @return {number}
 */
var pathSum = function (root, targetSum) {
    let map = {
        0: 1
    }
    let preSum = 0, count = 0
    const dfs = (node) => {
        if (node.left == null && node.right == null) {
            return
        }
        preSum += node.val
        if(map[preSum - targetSum]) count += map[preSum - targetSum]
        if(map[preSum]) map[preSum]++
        else map[preSum] = 1
        if (node.left)
            dfs(node.left)
        if (node.right)
            dfs(node.right)
        preSum -= node.val
        console.log(preSum);
    }
    dfs(root)
    return count
};